﻿$('#nav li').eq(0).addClass('active');

var alldata = [];

$('#calendar').fullCalendar({
  header: {
    left: '',
    center: 'title',
    right: 'today prev,next'
  },
  contentHeight: 450,
  handleWindowResize: false,
  eventLimit: true,
  monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
  monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
  dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
  dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
  today: ["今天"],
  firstDay: 1,
  titleFormat: 'YYYY年MM月',
  buttonText: {
    today: '本月',
    month: '月',
    week: '周',
    day: '日',
    prev: '上一月',
    next: '下一月'
  },
  /*loading: function (bool) {
   if (bool) {
   console.log('loading');
   } else {
   console.log('complete');
   }
   },*/
  dayClick: function (date, jsEvent, view) {//日期点击后弹出的jq ui的框，添加日程记录
    // console.log(alldata)
    var now = new Date();
    var day = new Date(date);
    var i = 0;
    var l = alldata.length;

    /*for (; i < l; i++) {
      var dates = new Date(alldata[i].DayInfo).pattern('yyyy-MM-dd');
      var days = day.pattern('yyyy-MM-dd');
      // console.log(date, days)
      if (dates === days && alldata[i].State === '已填') {
        window.location.href = '/Dept/Project/level31/' + new Date(dates).pattern('yyyy-MM-dd');
      }
    }*/

    // console.log(new Date(date).pattern('yyyy-MM-dd'))
     //var isToday = Date.parse(now.pattern('yyyy-MM-dd')) == Date.parse(day.pattern('yyyy-MM-dd'));
     //if (isToday) {
      window.location.href = '/Dept/Project/level31/' + new Date(date).pattern('yyyy-MM-dd');
     //}
    // window.location.href = '/PM/Project/indexl41/' + new Date(date).pattern('yyyy-MM-dd');
  },
  eventClick: function (calEvent, jsEvent, view) {
    // console.log(calEvent.title);
    // console.log(calEvent.content);
    // console.log(calEvent.start._d);
    // var state = calEvent.content.split('#-#')[1];
    /*var index = parseInt(calEvent.content, 10);
     var data = alldata[index];
     var date = new Date(data.DayInfo).pattern('yyyy-MM-dd');
     window.location.href = '/Dept/Project/level31/' + new Date(date).pattern('yyyy-MM-dd');*/
    // var TeacherScheduleID = data.TeacherScheduleID;
    // var ApplyID = data.ApplyID;
    // var ProjectID = data.ProjectID;
    // var url = '';
    // console.log(index, data)
    // if (data.State === '无状态') {
    //   url = '/Teacher/Project/indexl41/' + date + '=' + TeacherScheduleID + '=' + ProjectID;
    // } else {
    //   url = '/Teacher/Project/indexl42/' + date + '=' + ApplyID + '=' + ProjectID;
    // }
    // console.log(url)
    // window.open(url, '_self');

  },
  events: function (start, end, timezone, callback) {
    var d = new Date(end);
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = '01';
    if (month === 0) {
      year = year - 1;
      month = 12;
    }
    // var date = year + '-' + month + '-' + day + ' 00:00';
    // console.log(date);
    $.ajax({
      cache: false,
      async: false,
      url: '/api/CommonApi?API=ITGetMyDailyByMonth',
      type: 'get',
      data: {
        Year: year,
        Month: month
      },
      dataType: 'json',
      success: function (data) {
        // console.log(data);
        var now = new Date().pattern('yyyy-MM-dd');
        if (data.Success) {
          var o = data.ApiParamObj;
          alldata = o;
          var i = 0;
          var l = o.length;
          // var background = '#ffffff';
          for (; i < l; i++) {
            if (o[i].State !== '未填') {
              // background = '#f3f3f3';
              var color = 'rgba(244, 247, 245, 0.6)';
              var day = new Date(o[i].DayInfo).pattern('yyyy-MM-dd');
              if (now === day) {
                color = '#fff0d1';
              }
              $("#calendar").fullCalendar('renderEvent', {
                // id: data[key].Name.ID,
                // title: o[i].State,
                start: day,
                end: day,
                content: i,
                // content: o[i].TeacherScheduleID + '#-#' + o[i].ApplyID + '#-#' + o[i].ProjectID
                // overlap: false,
                className: 'noOpacity',
                rendering: 'background',
                color: color
              }, false);
            }

          }
          /*var o = data.ApiParamObj;
           alldata = o;
           var i = 0;
           var l = o.length;
           $("#calendar").fullCalendar('removeEvents');
           for (; i < l; i++) {
           // console.log(o[i].IsSchedule)
           if (o[i].IsSchedule !== '未排程') {
           $("#calendar").fullCalendar('renderEvent', {
           // id: data[key].Name.ID,
           title: o[i].IsSchedule + '-' + o[i].State,
           start: moment(new Date(o[i].DayInfo).pattern('yyyy-MM-dd 00:00')).format(),
           end: moment(new Date(o[i].DayInfo).pattern('yyyy-MM-dd 00:00')).format(),
           // content: o[i].TeacherScheduleID + '#-#' + o[i].ApplyID + '#-#' + o[i].ProjectID
           content: i
           }, false);
           }

           }

           $('[data-toggle="tooltip"]').tooltip();*/
        }
      }
    });
  },
  eventAfterRender: function (event, element, view) {
    // var index = parseInt(event.content, 10);
    // var data = alldata[index];
    // element.attr('title', 'Tooltip on left');
    // element.attr('data-toggle', 'tooltip');
    // element.attr('data-placement', 'top');

    // element.html(event.title + '- 项目名称' );
    // element.parent().css({position: 'relative'}).append(html)
  },
  eventMouseover: function (event, jsEvent, view) {
    // $(this).next().removeClass('none');
  },
  eventMouseout: function (event, jsEvent, view) {
    // $(this).next().addClass('none');
  }
});

